﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Xna.Framework.Media;

namespace VoiceRepeater
{
    public partial class PlayListPage : PhoneApplicationPage
    {
        private List<PlayList> playLists = new List<PlayList>();

        private int currentPlayList;

        public PlayListPage()
        {
            InitializeComponent();
        }

        public void AddPlayList(string name)
        {
            bool listExist = playLists.Any( list => 
                {
                    return list.Name == name;
                });
            if (!listExist)
            {
                playLists.Add(new PlayList(name));
            }
            else
            {
                // TODO: throw exception
            }

        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/FileBrowserPage.xaml", UriKind.Relative));
        }


        private void btnAddList_Click(object sender, RoutedEventArgs e)
        {
            currentPlayList = listPickerPlayList.Items.Count;
            listPickerPlayList.Items.Add("PlayList" + listPickerPlayList.Items.Count.ToString());
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (PhoneApplicationService.Current.State.ContainsKey("selectSongs"))
            {
                List<Song> selectSongs = PhoneApplicationService.Current.State["selectSongs"] as List<Song>;

                if ((selectSongs != null) && (selectSongs.Count > 0))
                {
                    playLists[currentPlayList].AddSongs(selectSongs);

                    List<string> total = (from song in selectSongs
                                          select song.Name).ToList();

                    listBoxSongs.ItemsSource = total;
                    
                }
            }


        }

        private void listBoxSongs_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void listBoxSongs_DoubleTap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            PhoneApplicationService.Current.State["currentSong"] = playLists[currentPlayList][listBoxSongs.SelectedIndex];
        }
    }
}